<template>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
      <div class="search-result-list">
        <!--
          父传子：子接、父传
          子传父 ：子派发、父监听 -->
          <van-list
            v-model="loading"
            :finished="done"
            finished-text="劳资是有底线的!"
            :immediate-check="false"
            offset="80"
            @load="pullUpLoadMoreFn"
            >
            <ArticleItem
              v-for="obj in resultList"
              :key="obj.art_id"
              :artObj="obj"
              :showCross="false"
            />
          </van-list>
      </div>
    </div>
</template>

<script>
import { getSearchResultListAPI } from '@/api/search'
export default {
  name: 'ResultList',
  data () {
    return {
      resultList: [], // 搜索结果列表
      loading: false, // 控制每一页的加载状态
      done: false, // 控制整个列表的加载状态
      page: 1// 分页的页码，默认值是1
    }
  },
  async created () {
    const resp = await getSearchResultListAPI({
      keyword: this.$route.params.name
    })
    this.resultList = resp.data.results
  },
  methods: {
    // 上拉触底加载更多
    async pullUpLoadMoreFn () {
      const resp = await getSearchResultListAPI({
        keyword: this.$route.params.name,
        page: ++this.page
      })
      const { results } = resp.data
      if (results.length < 10) {
        this.done = true
        return
      }
      this.resultList = [...this.resultList, ...results]
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
